package yxy.game.pm2.command.plainsCraft;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.module.rank.RankWorship;
import yxy.game.pm2.bean.module.rank.RankWorshipPlayer;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlainsCraftMessage;
import yxy.game.pm2.module.plainsCraft.RankPlainsCraft;
import yxy.game.pm2.module.player.PlayerHandle;

import java.util.Collection;

/** 中原争霸 中原争霸荣誉榜 */
@MessageType(proto = Command.PlainsCraftTop, classtype = PlainsCraftMessage.PlainsCraftTopRequest.class)
public class CMD_PlainsCraftTop extends Command<PlainsCraftMessage.PlainsCraftTopRequest> {

	@Override
	public void execute(MessageSource messageSource, PlainsCraftMessage.PlainsCraftTopRequest request) {
		String playerCode = messageSource.getTag();

		PlainsCraftMessage.PlainsCraftTopResponse.Builder response = PlainsCraftMessage.PlainsCraftTopResponse.newBuilder();

		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());

		PlayerHandle playerHandle = new PlayerHandle();
		// 5段前三
		{
			RankPlainsCraft rankPlainsCraft = new RankPlainsCraft(areaBean.getGroupId(), 5);
			Collection<RankEntry> top = rankPlainsCraft.top(3);

			RankWorship worship = rankPlainsCraft.getWorship();
			RankWorshipPlayer worshipPlayer = RankWorshipPlayer.get(playerCode, worship);

			int rank = 0;
			for (RankEntry entry : top) {
				String pCode = entry.getKey();

				PlainsCraftMessage.PlainsCraftRank.Builder builder = PlainsCraftMessage.PlainsCraftRank.newBuilder();

				builder.setRank(++rank);
				builder.setLevel(5);
				builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
				builder.setFans(worship.getScore(pCode));
				builder.setLiked(worshipPlayer.getWorshiped().contains(pCode));
				response.addRank(builder);
			}
		}

		// 4段第一
		{
			RankPlainsCraft rankPlainsCraft = new RankPlainsCraft(areaBean.getGroupId(), 4);
			Collection<RankEntry> top = rankPlainsCraft.top(1);

			RankWorship worship = rankPlainsCraft.getWorship();
			RankWorshipPlayer worshipPlayer = RankWorshipPlayer.get(playerCode, worship);

			int rank = 0;
			for (RankEntry entry : top) {
				String pCode = entry.getKey();

				PlainsCraftMessage.PlainsCraftRank.Builder builder = PlainsCraftMessage.PlainsCraftRank.newBuilder();

				builder.setRank(++rank);
				builder.setLevel(4);
				builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
				builder.setFans(worship.getScore(pCode));
				builder.setLiked(worshipPlayer.getWorshiped().contains(pCode));
				response.addRank(builder);
			}
		}
		// 3段第一
		{
			RankPlainsCraft rankPlainsCraft = new RankPlainsCraft(areaBean.getGroupId(), 3);
			Collection<RankEntry> top = rankPlainsCraft.top(1);

			RankWorship worship = rankPlainsCraft.getWorship();
			RankWorshipPlayer worshipPlayer = RankWorshipPlayer.get(playerCode, worship);

			int rank = 0;
			for (RankEntry entry : top) {
				String pCode = entry.getKey();

				PlainsCraftMessage.PlainsCraftRank.Builder builder = PlainsCraftMessage.PlainsCraftRank.newBuilder();

				builder.setRank(++rank);
				builder.setLevel(3);
				builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
				builder.setFans(worship.getScore(pCode));
				builder.setLiked(worshipPlayer.getWorshiped().contains(pCode));
				response.addRank(builder);
			}
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, PlainsCraftMessage.PlainsCraftTopResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}